package
{
	import com.bourre.commands.Delegate;
	import com.bourre.structures.Range;
	import com.kairos.commands.IntervalRT;
	import com.kairos.engine.RTBeacon;
	import com.kairos.utils.Monitor;
	import com.kairos.utils.MonitorFieldsList;
	
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.display.StageScaleMode;

	[SWF(width="500",height="130",frameRate="120",backgroundColor="#000000")]
	public class ParticleRTRunner extends Sprite
	{
		private var _oGenerator : IntervalRT;
		private var _dParticles : Dictionary;
		private var _oMonitor : Monitor;
		private var _oPauseMask : Sprite;
		
		[Embed(source="../lib/example-lib.swf", symbol="Button")]
		private var PauseMask : Class;
		
		public function ParticleRTRunner ()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			RTBeacon.getInstance().setMaxStepSize( 100 );
			RTBeacon.getInstance().setSmoothness( 10 );
			
			_dParticles = new Dictionary();
			_oGenerator = new IntervalRT ( new Delegate ( createParticle ), 50 );
			_oPauseMask = new PauseMask () as Sprite;
			_oMonitor = new Monitor ( 150, 80 );
			
			_oPauseMask.alpha = .5;
			
			_oMonitor.addField( MonitorFieldsList.FPS );
			_oMonitor.addField( new ParticleRTField ( "Particles", _dParticles, "particles", 0xffff9900, new Range ( 0, 100 ) ) );
			
			addChild( _oPauseMask );
			addChild( _oMonitor );
			
			addEventListener( MouseEvent.MOUSE_OVER, start );
			addEventListener( MouseEvent.MOUSE_OUT, stop );
			
			
		}
		
		public function start( e : Event = null ) : void
		{
			Mouse.hide();
			_oGenerator.start();
			_oMonitor.start();
			_oPauseMask.alpha = 0;
			
			for each ( var p : ParticleRT in _dParticles )
			{
				p.start();
			}
		}
		
		public function stop( e : Event = null ) : void
		{
			Mouse.show();
			_oGenerator.stop();
			_oMonitor.stop();
			_oPauseMask.alpha = .5;
			
			for each ( var p : ParticleRT in _dParticles )
			{
				p.stop();
			}
		}
		
		public function createParticle () : void
		{
			var scale : Number = .3 + Math.random() * .7;
			var p : ParticleRT = new ParticleRT(	
											this,
											170,
											120,
											Math.floor( ( 100 + Math.random() * 150 ) * (1-scale/2) ),
											Math.floor( ( -100 - Math.random() * 100 ) * (1-scale/2) ),
											Math.floor( 1000 + Math.random() * 2000 ),
											scale,
											removeParticle	);
			
			_dParticles[ p ] = p;			
			p.start();
		}
		
		public function removeParticle ( p : ParticleRT ) : void
		{
			p.stop();
			delete _dParticles[ p ];
		}
	}
}